package com.sun.stringescapeutils;

import com.sun.pojo.Type;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.util.stream.Collectors;

public class TypeService {
    FluxSink<Type> typeFluxSink;

    public void send(Type type) {
        typeFluxSink.next(type);
    }

    public void afterPropertiesSet() {
        Flux.<Type>create(emitter -> this.typeFluxSink = emitter, FluxSink.OverflowStrategy.IGNORE)
                .publishOn(Schedulers.boundedElastic())
                .buffer(Duration.ZERO)
                .map(types -> types.stream()
                        .collect(Collectors.groupingBy(Type::getType1, Collectors.groupingBy(Type::getType2)))
                )
                .subscribe(result -> result.forEach((key, value) -> System.out.println(key + "=" + value)));
    }

    public static void main(String[] args) {
        Type type1 = new Type("a", "A");
        Type type2 = new Type("a", "A");
        Type type3 = new Type("a", "B");
        Type type4 = new Type("a", "B");
        Type type5 = new Type("b", "C");
        Type type6 = new Type("b", "C");
        Type type7 = new Type("b", "D");
        Type type8 = new Type("b", "D");
        TypeService typeService = new TypeService();
        typeService.afterPropertiesSet();
        typeService.send(type1);
        typeService.send(type2);
        typeService.send(type3);
        typeService.send(type4);
        typeService.send(type5);
        typeService.send(type6);
        typeService.send(type7);
        typeService.send(type8);
        typeService.afterPropertiesSet();

       /* System.out.println("===================================================1111");
        List<Type> typeList = new ArrayList<>();
        typeList.add(type1);
        typeList.add(type2);
        typeList.add(type3);
        typeList.add(type4);
        typeList.add(type5);
        typeList.add(type6);
        typeList.add(type7);
        typeList.add(type8);

        Map<String, Map<String, List<Type>>> collect = typeList.stream()
                .collect(Collectors.groupingBy(Type::getType1, Collectors.groupingBy(Type::getType2)));
        System.out.println(collect.size());

        for (Map.Entry<String, Map<String, List<Type>>> stringMapEntry : collect.entrySet()) {
            System.out.println("==================================================================2222");
            System.out.println(stringMapEntry.getValue());
            System.out.println(stringMapEntry.getKey());
            System.out.println("==================================================================3333");
            for (Map.Entry<String, List<Type>> stringListEntry : stringMapEntry.getValue().entrySet()) {
                System.out.println(stringListEntry.getValue());
                System.out.println(stringListEntry.getKey());
            }
        }

        System.out.println("====================================================33333");
        Map<String, List<Type>> collect1 = typeList.stream()
                .collect(Collectors.groupingBy(Type::getType1));
        System.out.println(collect1.size());
        collect1.forEach((key, value) -> System.out.println(key + "=" + value));

        System.out.println("====================================================");
        Map<String, List<Type>> collect2 = typeList.stream()
                .collect(Collectors.groupingBy(Type::getType2));
        System.out.println(collect2.size());
        collect2.forEach((key, value) -> System.out.println(key + "=" + value));*/
    }

}
